package com.fuyun.rpc.server.protocol;

import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.net.NetServer;
import io.vertx.core.net.NetSocket;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author: zrq1351
 * @date: 2025/2/20
 */
@Slf4j
@AllArgsConstructor
public class VertxTcpServer implements Server {

	/**
	 * 启动服务
	 *
	 * @param port    端口
	 * @param handler 处理器
	 */
	@Override
	public void start(int port, Handler<?> handler) {
		Vertx vertx = Vertx.vertx();

		NetServer vertxNetServer = vertx.createNetServer();

		// 设置TCP连接处理器
		vertxNetServer.connectHandler((Handler<NetSocket>)handler);

		// 启动服务并监听端口
		vertxNetServer.listen(port, result -> {
			if (result.succeeded()) {
				log.info("VertxTcpServer start success");
			} else {
				log.error("VertxTcpServer start failed:{}", String.valueOf(result.cause()));
			}
		});

	}
}